/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.bpm.api.runtime.task.operator;

import com.je.bpm.api.runtime.task.impl.APITaskConverter;
import com.je.bpm.api.runtime.task.operator.desc.TaskCancelDelegateParamDesc;
import com.je.bpm.api.runtime.task.operator.validator.TaskCancelDelegateOperatorValidator;
import com.je.bpm.common.operation.OperatorEnum;
import com.je.bpm.engine.TaskService;
import com.je.bpm.engine.upcoming.ActivitiUpcomingRun;
import com.je.bpm.model.task.payloads.TaskCancelDelegatePayload;
import com.je.bpm.model.task.result.TaskResult;
import com.je.bpm.runtime.shared.operator.AbstractOperator;
import com.je.bpm.runtime.shared.operator.desc.OperationParamDesc;
import com.je.bpm.runtime.shared.operator.validator.OperatorPayloadParamsValidator;
import com.je.bpm.runtime.task.operator.TaskCancelDelegateOperator;

import java.util.Map;

/**
 * 取消委托操作
 */
public class TaskCancelDelegateOperatorImpl extends AbstractOperator<TaskCancelDelegatePayload, TaskResult> implements TaskCancelDelegateOperator {

    private APITaskConverter apiTaskConverter;
    private TaskService taskService;
    private OperatorPayloadParamsValidator<TaskCancelDelegatePayload> validator;
    private ActivitiUpcomingRun activitiUpcomingRun;

    public TaskCancelDelegateOperatorImpl(APITaskConverter apiTaskConverter, TaskService taskService, ActivitiUpcomingRun activitiUpcomingRun) {
        this.validator = new TaskCancelDelegateOperatorValidator();
        this.apiTaskConverter = apiTaskConverter;
        this.taskService = taskService;
        this.activitiUpcomingRun = activitiUpcomingRun;
    }

    @Override
    public String getId() {
        return OperatorEnum.TASK_CANCEL_DELEGATE_OPERATOR.getId();
    }

    @Override
    public String getName() {
        return OperatorEnum.TASK_CANCEL_DELEGATE_OPERATOR.getName();
    }

    @Override
    public OperationParamDesc getParamDesc() {
        return new TaskCancelDelegateParamDesc();
    }

    @Override
    public TaskResult execute(TaskCancelDelegatePayload taskCancelDelegatePayload) {
        taskService.cancelDelegateTask(taskCancelDelegatePayload.getTaskId(), taskCancelDelegatePayload.getProd(), taskCancelDelegatePayload.getBean());
        TaskResult result = new TaskResult(taskCancelDelegatePayload, null);
        return result;
    }

    @Override
    public TaskCancelDelegatePayload paramsParse(Map<String, Object> params) {
        TaskCancelDelegatePayload taskCancelDelegatePayload = new TaskCancelDelegatePayload(
                formatString(params.get("piid")),
                formatString(params.get(AbstractOperator.PROD)),
                formatString(params.get("taskId")),
                formatString(params.get("beanId")),
                params.get(AbstractOperator.BEAN) == null ? null : (Map<String, Object>) params.get(AbstractOperator.BEAN));
        return taskCancelDelegatePayload;
    }

    @Override
    public OperatorPayloadParamsValidator<TaskCancelDelegatePayload> getValidator() {
        return validator;
    }

}
